/*
 * Copyright (C), 百联集团
 * FileName: MemberLevelConcurrency.java
 * Author:   hudepin
 * Date:     2017年12月20日 下午2:10:29
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.deep.test.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * Description:多线程处理会员等级
 * </p>
 * 
 * @author hudepin
 * @date 2017年12月20日 下午2:10:29
 * @version 1.0
 * 
 */
public class MemberLevelConcurrency {
	public static void main(String[] args) throws InterruptedException, ExecutionException {

		ThreadPoolExecutor executor = new ThreadPoolExecutor(20, 50, 1, TimeUnit.SECONDS, new LinkedBlockingQueue());
		List<Future<List<User>>> futures = new ArrayList<Future<List<User>>>(100);
		// 模拟处理会员等级信息
		List<User> users = genUserList();
		long startTime = System.currentTimeMillis();
		// 一个任务处理1000条数据
		int dataLimitSize = 100;
		int size = users.size();
		if (dataLimitSize < size) {
			int part = size / dataLimitSize;
			System.out.println("共有 ： " + size + "条，！" + " 分为 ：" + part + "批");
			for (int i = 0; i < part; i++) {
				List<User> subUsers = users.subList(i*dataLimitSize, (i+1)*dataLimitSize);
				futures.add(executor.submit(new DoudouLevelExecutor(subUsers)));
			}
		}
		// 提交所有的任务后,关闭executor
		System.out.println("Starting shutdown executor...");
//		int errorCount = 0;
//		int successCount = 0;
//		for (Future<List<User>> future : futures) {
//			List<User> list = future.get();
//			if (list != null) {
//				successCount++;
//			} else {
//				errorCount++;
//			}
//		}
//		System.out.println(successCount + " data successfully process, but " + errorCount + " failed.");
		executor.shutdown();
		// 每秒钟打印执行进度
		while (!executor.isTerminated()) {
			executor.awaitTermination(500, TimeUnit.MILLISECONDS);
			int progress = Math.round((executor.getCompletedTaskCount() * 100) / executor.getTaskCount());
			System.out.println(progress + "% done (" + executor.getCompletedTaskCount() + " users have been process).");
		}
		//  看成功处理了多少记录
//		int errorCount = 0;
//		int successCount = 0;
//		for (Future<List<User>> future : futures) {
//			List<User> list = future.get();
//			if (list != null) {
//				successCount++;
//			} else {
//				errorCount++;
//			}
//		}
//		System.out.println(successCount + " emails were successfully sent, but " + errorCount + " failed.");
		System.out.println("time:" + (System.currentTimeMillis() - startTime) / 1000 + "s");
	}

	private static List<User> genUserList() {
		List<User> list = new ArrayList<User>(10000);
		long i = 0;
		for (; i < 10000; i++) {
			User u = new User();
			u.setMemberId(i);
			u.setMemberLevel("20");
			u.setDoudouLevel("102");
			list.add(u);
		}
		return list;
	}
}
